/*
 * Package il.ac.biu.cs.grossmm.impl.activeData
 * File PublicationPointBase.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.api.flow;

import il.ac.biu.cs.grossmm.api.data.NodeType;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;

import java.util.HashSet;
import java.util.Set;

/**
 * Base class for publication point implementation.
 * 
 * @param <N>
 */
public abstract class PublicationPointBase<N> implements PublicationPoint<N>,
		NotificationProvider<N> {
	/**
	 * key attern for this point
	 */
	public final KeyPattern pattern;

	/**
	 * node type for this pooint
	 */
	public final NodeType<N> nodeType;

	/**
	 * Set of names of this publication point
	 */
	protected final Set<String> names;

	/**
	 * Given a node type, key pattern and a name, constructs a new publication
	 * point base.
	 * 
	 * @param nodeType
	 *            node type for the new publication point base
	 * @param pattern
	 *            key pattern for the new publication point base
	 * @param name
	 *            publication point name
	 */
	public PublicationPointBase(NodeType<N> nodeType, KeyPattern pattern,
			String name) {
		this.pattern = pattern;
		this.nodeType = nodeType;

		this.names = new HashSet<String>();
		names.add(name);
	}

	/**
	 * Checks whether the specified name is one of names of this publication
	 * point
	 * 
	 * @param name
	 *            the name
	 * @return true iff the specified name is one of names of this publication
	 *         point
	 */
	boolean hasName(String name) {
		return names.contains(name);
	}

	/**
	 * Adds a new name for this publication point
	 * 
	 * @param name
	 *            the new name for this publication point
	 */
	public void addName(String name) {
		names.add(name);
	}

	/**
	 * Removes a name for this publication point
	 * 
	 * @param name
	 *            the name to remove
	 */
	public void removeName(String name) {
		names.remove(name);
	}

	/**
	 * Checks whether this point is reachable by specifying some name
	 * 
	 * @return true iff this point is reachable by specifying some name
	 */
	public boolean isReachable() {
		return !names.isEmpty();
	}

	/**
	 * Matches a key pattern and a node type pair agains this publication point,
	 * return true only on exact match.
	 * 
	 * @param pattern
	 *            a key pattern
	 * @param nodeType
	 *            a data node type
	 * @return true iff the specified key pattern and node type are equal to
	 *         those of this publication point
	 */
	public boolean matches(KeyPattern pattern, NodeType<N> nodeType) {
		return this.pattern.equals(pattern) && nodeType == this.nodeType;
	}

	/**
	 * Returns priority 1.0 (all publication points have priority 1.0)
	 */
	public final double getPriority() {
		return 1.0;
	}
}
